﻿using MengCC.Core.App;
using MengCC.Core.Data;
using MengCC.Core.IO;
using MengCC.Core.Plugins;
using MengCC.Core.Projects;
using MengCC.Core.Works;
using MengCC.Framework;
using MengCC.Framework.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class FrameworkService
    {
        public static void AddCCService(this IServiceCollection services,Action<CCServiceOptions> options=null)
        {
            var ops = new CCServiceOptions();
            options?.Invoke(ops);

            services.AddScoped<AppManager>();
            services.AddScoped<IOManager>();
            services.AddScoped<WorkManager>(s=>new WorkManager(s.GetService<PluginManager>(),s.GetService<ProjectManager>(),s.GetService<IOManager>(), ops.WorkspaceRootDir ?? Path.Combine(Environment.CurrentDirectory, "WorkSpaces")));
            services.AddScoped<ProjectManager>();
            services.AddScoped<PluginManager>(s=> new PluginManager(ops.PluginRootDir?? Path.Combine(Environment.CurrentDirectory, "Plugins")));
            services.AddScoped<Project>();
            services.AddScoped<BuildLog>();
            services.AddScoped(typeof(IBaseRepository<,>), typeof(BaseRepository<,>));

        }

        public  static void UseCCService(this IApplicationBuilder app, IServiceProvider serviceProvider)
        {
            var manager = serviceProvider.GetService<AppManager>();
            manager.Start();
        }
    }
}
